package com.cirrusblue.caching.roo.addon;

import org.springframework.roo.addon.beaninfo.BeanInfoMetadata;
import org.springframework.roo.addon.entity.EntityMetadata;
import org.springframework.roo.addon.finder.FinderMetadata;
import org.springframework.roo.classpath.PhysicalTypeIdentifier;
import org.springframework.roo.classpath.PhysicalTypeMetadata;
import org.springframework.roo.classpath.itd.AbstractItdMetadataProvider;
import org.springframework.roo.classpath.itd.ItdTypeDetailsProvidingMetadataItem;
import org.springframework.roo.metadata.MetadataDependencyRegistry;
import org.springframework.roo.metadata.MetadataService;
import org.springframework.roo.model.JavaType;
import org.springframework.roo.process.manager.FileManager;
import org.springframework.roo.project.Path;
import org.springframework.roo.support.lifecycle.ScopeDevelopment;
import org.springframework.roo.support.util.Assert;

/**
 * Provides {@link CacheMetadata}.
 * 
 * @author 
 * @since 1.0
 *
 */
@ScopeDevelopment
public final class CacheMetadataProvider extends AbstractItdMetadataProvider {
	
	private CachingOperations cachingOperations;
	
	public CacheMetadataProvider(MetadataService metadataService, MetadataDependencyRegistry metadataDependencyRegistry, FileManager fileManager, CachingOperations cachingOperations) {
		super(metadataService, metadataDependencyRegistry, fileManager);
		Assert.notNull(cachingOperations, "Cache operations required");
		addMetadataTrigger(new JavaType(RooCache.class.getName()));
		this.cachingOperations = cachingOperations;
	}
	
	protected ItdTypeDetailsProvidingMetadataItem getMetadata(String metadataIdentificationString, JavaType aspectName, PhysicalTypeMetadata governorPhysicalTypeMetadata, String itdFilename) {
		// We know governor type details are non-null and can be safely cast
				
		return new CacheMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, metadataService);
	}
	
	public String getItdUniquenessFilenameSuffix() {
		return "Cache";
	}

	protected String getGovernorPhysicalTypeIdentifier(String metadataIdentificationString) {
		JavaType javaType = CacheMetadata.getJavaType(metadataIdentificationString);
		Path path = CacheMetadata.getPath(metadataIdentificationString);
		String physicalTypeIdentifier = PhysicalTypeIdentifier.createIdentifier(javaType, path);
		return physicalTypeIdentifier;
	}

	protected String createLocalIdentifier(JavaType javaType, Path path) {
		return CacheMetadata.createIdentifier(javaType, path);
	}

	public String getProvidesType() {
		return CacheMetadata.getMetadataIdentiferType();
	}
}
